/*
	This file is part of SRServerLibrary.
	Foobar is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	SRServerLibrary is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with SRServerLibrary.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Plugin.h"
#include <iostream>
#include <vector>
#include "MBItem.h"
#include "stdlib.h"
#include "../../include/global.h"
#include <QDebug>


MBItem *currentItem;


Plugin::Plugin()
{
	
}


int Plugin::initiate()
{
	currentItem = new MBItem("/Users/niklas/code", "");
	currentItem->open(); // load it
	return 0;
}


int Plugin::destroy()
{
	delete currentItem;
	
	return 0;
}


char* Plugin::doCommand(char *command)
{
	vector<string> commandSplitted = delimiterSplit((string)command, commandArgumentSeparator);
	qDebug() << command << endl;
	qDebug() << "commandSplitted contains " << commandSplitted.size() << " elements. delimiter: " << commandArgumentSeparator << endl;
	
	qDebug() << "item count: " << currentItem->itemCount() << endl;
	
	if(commandSplitted.at(0).compare("open") == 0)
	{
		qDebug() << "opening item " << atoi(commandSplitted.at(1).c_str()) << endl;
		// hmm is this better than getting the reference straight away?
		MBItem temp = currentItem->getItemAt(atoi(commandSplitted.at(1).c_str()));
		currentItem = &temp;
		qDebug() << "asd" << endl;
	}
	else if(commandSplitted.at(0).compare("list") == 0)
	{
		// do nothing. just let it return the listing of the current item
	}
	else
	{
		// TODO: handle unrecognized commands here
	}
	
	char *commandResult = (char*)currentItem->open().c_str();
	
	return commandResult;
}

Q_EXPORT_PLUGIN2(plugin, Plugin);
